#pragma once
#include <iostream>
#include <string>
#include <memory>
#include <jsoncpp/json/json.h>
enum
{
    // 除零
    DIV_ZERO = 1,
    // 模0
    MOD_ZERO,
    DATAOVERFLOW, // 溢出
    Illegal_Operations
};
static const std::string &sep = "\r\n";//约定的协议分割标记

// 报文约定："len"\r\n{Json}\r\n
std::string Encode(const std::string &jsonstr) // 构造完整的报文
{
    // 添加报头
    int Jsonlen = jsonstr.size();
    return std::to_string(Jsonlen) + sep + jsonstr + sep;
}

std::string Decode(std::string &packpagestream)
{
    // 只有当packpagestream的size >= 一次完整的报文长度时，才返回上层
    auto pos = packpagestream.find(sep);
    if (pos == std::string::npos)
        return std::string(); // packpagestream未读取完成
    std::string Jsonlenstr = packpagestream.substr(0, pos);
    int jsonlen = std::stoi(Jsonlenstr);
    // 计算出一次报文的总长度
    int total = Jsonlenstr.size() + jsonlen + 2 * sep.size();
    // 判断packpagestream的len 是否 >= total
    if (packpagestream.size() < total)
        return std::string(); // packpagestream未读取完成
    // 截取一次报文
    std::string jsonstring = packpagestream.substr(pos + sep.size(), total);
    // 从packpagestream中删除截取出来的一次报文
    packpagestream.erase(0,total);
    return jsonstring;
}
class Request
{
public:
    Request() {}
    Request(int x, int y, char oper) : _x(x), _y(y), _oper(oper)
    {
    }
    bool Serialization(std::string *out) // 序列化
    {
        Json::Value root;
        root["x"] = _x;
        root["y"] = _y;
        root["oper"] = _oper;
        Json::FastWriter writer;
        *out = writer.write(root);
        return true;
    }
    bool Deserialization(const std::string &in) // 反序列化
    {
        Json::Value root;
        Json::Reader reader;
        reader.parse(in, root);
        _x = root["x"].asInt();
        _y = root["y"].asInt();
        _oper = root["oper"].asInt();
        return true;
    }
    void SetValue(int x, int y, char oper)
    {
        _x = x;
        _y = y;
        _oper = oper;
    }
    int GetX()
    {
        return _x;
    }
    int GetY()
    {
        return _y;
    }
    char GetOper()
    {
        return _oper;
    }
    ~Request()
    {
    }

private:
    int _x;
    int _y;
    char _oper;
};

class Respence
{
public:
    Respence() : _result(0), _CodeStatus(0), _desc("success!")
    {
    }
    bool Serialization(std::string *out) // 序列化
    {
        Json::Value root;
        root["result"] = _result;
        root["CodeStatus"] = _CodeStatus;
        root["desc"] = _desc;
        Json::FastWriter writer;
        *out = writer.write(root);
        Json::StyledWriter write1;
        return true;
    }
    bool Deserialization(const std::string &in) // 反序列化
    {
        Json::Value root;
        Json::Reader reader;
        reader.parse(in, root);
        _result = root["result"].asInt();
        _CodeStatus = root["CodeStatus"].asInt();
        _desc = root["desc"].asCString();
        return true;
    }
    void ModifyResult(int result)
    {
        _result = result;
    }
    void ModifyStatus(const std::string &status, int STATUS)
    {
        _CodeStatus = STATUS;
        _desc = status;
    }
    void PrintResult()
    {
        std::cout << "result: " << _result << ", code: " << _CodeStatus << ", desc: " << _desc << std::endl;
    }
    ~Respence() {}

private:
    int _result;
    int _CodeStatus;
    std::string _desc; // 结果状态的描述
};

// 设计模式--工厂模式

class Factor
{
public:
    static std::shared_ptr<Request> BuildRequestDefault()
    {
        return std::make_shared<Request>();
    }
    static std::shared_ptr<Respence> BuildRespenceDefault()
    {
        return std::make_shared<Respence>();
    }
};
